<div class="open-form">
  <div class="open-model">
    <div class="open-auto">
      <!-- 查询条件 -->
      <form lay-filter="orderFormTableForm" id="orderFormTableForm" class="layui-form">
        <div class="layui-form-item layui-row lay-query-box">
          <input name="id" type="hidden" />
          <div class="layui-inline layui-col-md3">
            <label class="layui-form-label">产品编码：</label>
            <div class="layui-input-block">
              <input name="cusSkuCode" class="layui-input" type="text" />
            </div>
          </div>
          <div class="layui-inline layui-col-md3">
            <label class="layui-form-label">产品名称：</label>
            <div class="layui-input-block">
              <input name="cusProductName" class="layui-input" type="text" />
            </div>
          </div>
          <div class="layui-inline layui-col-md3">
            <label class="layui-form-label">产品类型：</label>
            <div class="layui-input-block">
              <input id="proTypeId" name="proTypeId" type="hidden" />
              <div class="layui-input-inline mr0" id="proTypeIdType"></div>
            </div>
          </div>
          <div class="query-rt">
            <div class="layui-inline">
              <button type="button" class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit type="button">查询</button>
              <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit type="button">重置</button>
            </div>
          </div>
        </div>
      </form>
      <button class="layui-btn" id="isCheckFun" style="border-radius: 4px; margin-bottom: 10px">确认选择</button>
      <table id="orderFormTableTable"></table>
    </div>
    <div class="layui-form-item text-right">
      <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
    </div>
  </div>
</div>
<script>
  layui.use(["layer", "form", "table", "admin", "upload", "tree", "laytpl"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    const nowData = { ...parentData };
    const id = nowData.id;

    admin.initDataSelectTree(true, "enterprise/product/admin/productType/getAllTreeData", null, "proTypeIdType", null, "物料分类", "proTypeId", null);
    if (id) {
      table.render({
        elem: "#orderFormTableTable",
        url: config.base + `enterprise/sell/admin/customerProductCatalog/list`,
        where: {
          customerId: id,
          notInListIds: nowData.cacheList.map((v) => v.cusProductId).join(","),
          ...(nowData.where ? nowData.where : {})
        },
        page: true,
        cellMinWidth: 100,
        height: 600,
        cols: [
          [
            { type: "checkbox", fixed: "left" },
            { type: "numbers", title: "序号" },
            { field: "cusSkuCode", align: "center", title: "客户产品编码", width: 150 },
            { field: "cusProductName", align: "center", title: "客户产品名称", width: 140 },
            { field: "cusProductSpec", align: "center", title: "客户产品规格码", width: 140 },
            { field: "attachmentName", align: "center", title: "附件" },
            { field: "proTypeName", align: "center", title: "内部物料类型", width: 140 },
            { field: "skuCode", align: "center", title: "内部物料规格码", width: 140 },
            { field: "unitName", align: "center", title: "主计量单位" },
            { field: "remark", align: "center", title: "备注" }
          ]
        ],
        done: function () {
          // if (nowData.cacheList.length > 0) {
          //     table.cache['orderFormTableTable'].forEach((item, index) => {
          //         const isSelected = nowData.cacheList.some(checkItem => checkItem.cusProductId === item.cusProductId);
          //         if (isSelected) {
          //             item["LAY_CHECKED"] = true
          //             $('div.layui-table-body input[name="layTableCheckbox"]:eq(' + (index) + ')').prop('checked', true);
          //             $('div.layui-table-body input[name="layTableCheckbox"]:eq(' + (index) + ')').next().addClass('layui-form-checked');
          //
          //         }
          //     });
          // }
          // form.render('checkbox');
        }
      });
      // admin.req(`enterprise/sell/admin/customerProductCatalog/list`, {
      //     customerId: id,
      //     notInListIds: nowData.cacheList.map(v => {
      //         if (v.fid) return v.fid
      //         if (v.id) return v.id
      //     }).join(',')
      // }, (res) => {
      //     // console.log(res.data.map(v => {
      //     //     delete v.id
      //     //     return v
      //     // }))
      //     table.reload('orderFormTableTable', {
      //         data: res.data.map(v => {
      //             v.fid = v.id
      //             delete v.id
      //             return v
      //         })
      //     })
      // })

      $("#isCheckFun").click(() => {
        var checkStatus = table.checkStatus("orderFormTableTable"); // 'demo'是表格的ID
        var data = checkStatus.data; // 选中的数据
        if (typeof parent.layui !== "undefined") {
          childData.list = data.map((v) => {
            v.customerSkuItem = v.cusSkuCode;
            v.customerSkuName = v.cusProductName;
            v.customerSpecCode = v.cusProductSpec;
            delete v.id;
            return v;
          });
          layer.close(parentData.zindex); // 关闭指定的弹窗
        }
      });
      form.on("submit(formSubSearchLog)", function (data) {
        table.reload("orderFormTableTable", {
          where: data.field
        });
      });
      //resets
      form.on("submit(resets)", function (data) {
        admin.resetSearch(data.field);
        table.reload("orderFormTableTable", {
          where: data.field
        });
      });
    }
  });
</script>
